/**
 * Copyright 2018
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.renren.common.validator;

import io.renren.common.exception.RRException;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * 数据校验
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:50
 */
public abstract class Assert {

	public static void hasText(String str, String message) {
		if (!StringUtils.hasText(str)) {
			throw new RRException(message);
		}
	}

	public static void notNull(Object object, String message) {
		if (object == null) {
			throw new RRException(message);
		}
	}

	public static void isTrue(boolean expression, String message) {
		if (!expression) {
			throw new RRException(message);
		}
	}

	public static void isNull(Object object, String message) {
		if (object != null) {
			throw new RRException(message);
		}
	}

	public static void doesNotContain(String textToSearch, String substring, String message) {
		if (StringUtils.hasLength(textToSearch) && StringUtils.hasLength(substring) && textToSearch.contains(substring)) {
			throw new RRException(message);
		}
	}

	public static void notEmpty(Object[] array, String message) {
		if (ObjectUtils.isEmpty(array)) {
			throw new RRException(message);
		}
	}

	public static void noNullElements(Object[] array, String message) {
		if (array != null) {
			for (Object element : array) {
				if (element == null) {
					throw new RRException(message);
				}
			}
		}
	}

	public static void notEmpty(Collection<?> collection, String message) {
		if (CollectionUtils.isEmpty(collection)) {
			throw new RRException(message);
		}
	}

	public static void notEmpty(Map<?, ?> map, String message) {
		if (CollectionUtils.isEmpty(map)) {
			throw new RRException(message);
		}
	}

	public static final List<String> IMG_LIST = Arrays.asList(".jpg", ".gif", ".png", ".bmp", ".jpeg", "jpg", "gif", "png", "bmp", "jpeg");

	public static void isImage(String suffix, String message) {
		if (suffix == null || suffix.isEmpty() || !IMG_LIST.contains(suffix.toLowerCase())) {
			throw new RRException(message);
		}
	}
}
